<template>
  <div v-if="buyInfo">
    <div v-if="superManager && list.length > 1">
      <choose-view :list="list" :selected="selectTeam" @sendChoose="chooseClick"></choose-view>
      <div style="position: fixed; top: 48px; z-index: 999;">
        <university-tab :selected="currentTab" @selectTab="onTabSelected"></university-tab>
      </div>
      <div style="height: 98px"></div>
    </div>
    <div v-else>
      <div style="position: fixed; top: 0; z-index: 999;">
        <university-tab :selected="currentTab" @selectTab="onTabSelected"></university-tab>
      </div>
      <div style="height: 49px"></div>
    </div>
    <router-view :teamId="selectTeam.id" :tryStatus="buyInfo.subscriptionId === 45"/>

  </div>
</template>

<script>
  import ChooseView from './components/UniversityChooseView'
  import UniversityTab from './components/UniversityTab'
  import EnterpriseAPI from '@/repo/api/EnterpriseLevelAPI'
  import {userActionsTypes} from "@/repo/store/user";
  import {universityActionTypes} from "../../../repo/store/university";

  export default {
    name: "home.vue",
    data() {
      return {
        //是否为为超级管理员
        superManager: false,
        currentTab: 0,
        list: [],
        selectTeam: {},
        buyInfo: null,
      }
    },
    components: {
      UniversityTab,
      ChooseView
    },
    activated() {
      switch (this.currentTab) {
        case 0:
          this.$_router.replace('/university/chart')
          break
        case 1:
          this.$_router.replace('/university/ranking')
          break
        case 2:
          this.$_router.replace('/university/team')
          break
      }
    },
    created() {
      if (this.$route.path.includes('/university/chart')) {
        this.currentTab = 0
      } else if (this.$route.path.includes('/university/ranking')) {
        this.currentTab = 1
      } else if (this.$route.path.includes('/university/team')) {
        this.currentTab = 2
      } else {
        this.currentTab = 0
      }
    },
    mounted() {
      this.initRoot()
    },
    methods: {
      onTabSelected(data) {
        this.currentTab = data.tab
      },
      //点击选择企业
      chooseClick(el) {
        this.selectTeam = el
      },
      initRoot() {

        Promise.all([
          this.$store.dispatch(userActionsTypes.FETCH_ENTERPRISE_INFO),
          EnterpriseAPI.getGroupLevelInfo()
        ]).then((res) => {

          let enterpriseData = res[0]
          this.selectTeam = enterpriseData[0].enterprise
          this.superManager = enterpriseData[1].roleType === 2
          this.buyInfo = res[1].responseData.buyInfo

          if (this.superManager) {
            this.getTeamList()
          }
        })


      },
      getTeamList() {
        this.$store.dispatch(universityActionTypes.UPDATE_UNIVERSITY_ENTERPRISE_LIST).then((res) => {
          this.list = res
        })
      }
    }
  }
</script>

<style scoped>

</style>
